<?php
/**
 * 地址信息服务
 *
 * User: Jay
 * Date: 2018/8/3
 */

namespace ServiceBundle\Controller;

use BaseBundle\Controller\BaseController;
use ServiceBundle\Model\m_area;

class AreaController extends BaseController
{
    /**
     * 全部的省市区数据
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function allAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $area = new m_area($this->container);

        $data['info'] =  $area->getAll();

        return $this->response('成功得到数据', 0, $data);
    }

    /**
     * 得到下辖省市区信息
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function subordinationAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $area = new m_area($this->container);

        $area_id = self::$request->get('area_id', 0);

        if(!is_numeric($area_id) || $area_id < 0){
            return $this->response('area_id：' . $area_id . ' 不合法');
        }

        $data['info'] =$area->getSubordination($area_id);

        return $this->response('成功得到数据', $this->getParameter('error_code_success'), $data);
    }

    /**
     * 根据城市名称得到城市ID
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function getCityIdAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $area = new m_area($this->container);

        $city_name = self::$request->get('city_name');

        if(empty($city_name)){
            return $this->response('城市名不能为空');
        }

        $data['city_id'] = $area->getCityId($city_name);

        return $this->response('成功得到数据', 0, $data);
    }

    /**
     * 得到全部的城市
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function getAllCityAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $area = new m_area($this->container);

        return $this->response('成功得到数据', 0, $area->getAllCity());
    }
}